package com.xin.springcloudconsumer.controller;

import com.xin.springcloudconsumer.config.ProjectUrlConfig;
import com.xin.springcloudconsumer.utils.SessionValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/ftl")
@Slf4j
public class FtlController {
    @Autowired
    public ProjectUrlConfig projectUrlConfig;


    @GetMapping("/add")
    public ModelAndView add(Map<String,Object> map,
                            HttpServletRequest request){
        String headImage = SessionValueUtils.getHeadImage(request);
        map.put("headImage",headImage);
        return new ModelAndView("detail/add",map);
    }
    //微信验证
    @GetMapping("/auth")
    public ModelAndView auth(){
        log.info("调用登录");
        String returnUrl=projectUrlConfig.localUrl.concat("/ftl/success");
        String url=projectUrlConfig.wxAuthUrl.concat("/wx/authorize").concat("?returnUrl=").concat(returnUrl);
        return new ModelAndView("redirect:".concat(url));
    }

    @GetMapping("/success")
    public ModelAndView success(@RequestParam(name = "result",defaultValue = "") String result,
                                HttpServletRequest request){
        String[] split = result.split(",");
        String openId=split[0];
        String headImage=split[1];
        request.getSession().setAttribute("openId",openId);
        request.getSession().setAttribute("headImage",headImage);
        return new ModelAndView("redirect:/event/findAll");
    }
}
